package com.zp.drug.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.api.bo.DrugStorageDeptBo;
import com.zp.drug.api.vo.DrugStorageDeptVo;
import com.zp.drug.service.IDrugStorageDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 药品库存单位字典控制器
 * 前端访问路由地址为:/drug/drugStorageDept
 *
 * @author zhang peng
 * @ date 2023-06-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugStorageDept")
public class DrugStorageDeptController extends BaseController {

    private final IDrugStorageDeptService iDrugStorageDeptService;

    /**
     * 查询药品库存单位字典列表 (分页)
     */
    @SaCheckPermission("base:drugStorageDept:list")
    @GetMapping("/selectPage")
    public TableDataInfo<DrugStorageDeptVo> selectPage (DrugStorageDeptBo bo, PageQuery pageQuery) {
        return iDrugStorageDeptService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询药品库存单位字典列表 (不分页)
     */
    @SaCheckPermission("base:drugStorageDept:list")
    @GetMapping("/list")
    public R<List<DrugStorageDeptVo>> list (DrugStorageDeptBo bo) {
        List<DrugStorageDeptVo> list = iDrugStorageDeptService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出药品库存单位字典列表
     */
    @SaCheckPermission("base:drugStorageDept:export")
    @Log(title = "药品库存单位字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (DrugStorageDeptBo bo, HttpServletResponse response) {
        List<DrugStorageDeptVo> list = iDrugStorageDeptService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品库存单位字典", DrugStorageDeptVo.class, response);
    }

    /**
     * 获取药品库存单位字典详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:drugStorageDept:list")
    @GetMapping("/{id}")
    public R<DrugStorageDeptVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iDrugStorageDeptService.queryById(id));
    }

    /**
     * 新增药品库存单位字典
     */
    @SaCheckPermission("base:drugStorageDept:add")
    @Log(title = "药品库存单位字典", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody DrugStorageDeptBo bo) {
        return toAjax(iDrugStorageDeptService.insertByBo(bo) ? 1 : 0);
    }


    /**
     * 以科室id查询对应的药房、药库信息
     */
    @PostMapping("/getDrugVsDeptList")
    public R<List<DrugStorageDeptVo>> getDrugVsDeptList (@Validated(AddGroup.class) @RequestBody DrugStorageDeptBo bo) {
        return R.ok(iDrugStorageDeptService.getDrugVsDeptList(bo));
    }

    /**
     * 修改药品库存单位字典
     */
    @SaCheckPermission("base:drugStorageDept:edit")
    @Log(title = "药品库存单位字典", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody DrugStorageDeptBo bo) {
        return toAjax(iDrugStorageDeptService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品库存单位字典
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:drugStorageDept:remove")
    @Log(title = "药品库存单位字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iDrugStorageDeptService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @GetMapping("/refreshCacheDrugStorageDept")
    public R<String> refreshCacheDrugStorageDept () {
        iDrugStorageDeptService.refreshCacheDrugStorageDept();
        return R.ok("刷新成功");
    }
}
